//
//  MontherViewController.m
//  Mother
//
//  Created by 郭明皓 on 16/6/20.
//  Copyright © 2016年 郭明皓. All rights reserved.
//

#import "MontherViewController.h"
#import "MontherLookTableViewCell.h"
#import "MontherDoTableViewCell.h"
#import "SecondTableViewCell.h"
@interface MontherViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UIScrollView *scroll;
@property (nonatomic, strong)UITableView *MontherLookTableview;
@property (nonatomic, strong)UITableView *MontherDoTableview;
@property (nonatomic, strong)UITableView *MontherReadTableview;

@property (nonatomic, strong)UIView *MontherLookHeaderview;

@property (nonatomic, strong)UIScrollView *scrollView;   //轮播图
@property (nonatomic, strong)UIScrollView *titleScrollview;
@property (nonatomic, strong)UIButton *MontherLookButton;
@property (nonatomic, strong)UIButton *MontherDoButton;
@property (nonatomic, strong)UIButton *MontherReadButton;
@property (nonatomic, assign)BOOL isClicked;

@property (nonatomic, strong)NSMutableArray *picArr;
@end

@implementation MontherViewController

-(instancetype )initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.picArr = [NSMutableArray arrayWithObjects:@"h1.jpeg", @"h2.jpeg", @"h3.jpeg", @"h4.jpeg", @"h5.jpeg", nil];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    //titileScrollview
    self.titleScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 0, 200, 49)];
    self.navigationItem.titleView = _titleScrollview;
    _titleScrollview.contentSize = CGSizeMake(3 * 70, 0);
    _titleScrollview.pagingEnabled = YES;
    _titleScrollview.delegate = self;
    
    _MontherLookButton = [UIButton addBtnImage:nil AndFrame:CGRectMake(0, 0, 70, 49) WithTarget:self action:@selector(LookClick:)];
    [_MontherLookButton setTitle:@"妈妈看" forState:UIControlStateNormal];
    _MontherLookButton.titleLabel.font =[UIFont systemFontOfSize:12 weight:0.5];
    [_titleScrollview addSubview:_MontherLookButton];
    [_MontherLookButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    _MontherDoButton = [UIButton addBtnImage:nil AndFrame:CGRectMake(70, 0, 70, 49) WithTarget:self action:@selector(DoClick:)];
    [_MontherDoButton setTitle:@"妈妈做" forState:UIControlStateNormal];
    _MontherDoButton.titleLabel.font =[UIFont systemFontOfSize:12 weight:0.5];
    [_titleScrollview addSubview:_MontherDoButton];
    [_MontherDoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    _MontherReadButton = [UIButton addBtnImage:nil AndFrame:CGRectMake(140, 0, 70, 49) WithTarget:self action:@selector(readClick:)];
    [_MontherReadButton setTitle:@"妈妈读" forState:UIControlStateNormal];
    _MontherReadButton.titleLabel.font =[UIFont systemFontOfSize:12 weight:0.5];
    [_titleScrollview addSubview:_MontherReadButton];
    [_MontherReadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    //scrollview
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 113)];
    
    [self.view addSubview:self.scroll];
    self.scroll.contentSize = CGSizeMake(3 * WIDTH, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    self.scroll.backgroundColor = [UIColor yellowColor];
    self.scroll.layer.borderWidth = 1;

    
    //MontherLook
    self.MontherLookTableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, _scroll.frame.size.height) style:UITableViewStyleGrouped];
    [self.scroll addSubview:_MontherLookTableview];
    self.MontherLookTableview.delegate = self;
    self.MontherLookTableview.dataSource = self;
    self.MontherLookTableview.backgroundColor = [UIColor greenColor];
    self.MontherLookTableview.rowHeight = 450;
    
    self.MontherLookHeaderview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    self.MontherLookTableview.tableHeaderView = _MontherLookHeaderview;
    
    //MontherDo
    self.MontherDoTableview = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, _scroll.frame.size.height) style:UITableViewStylePlain];
    [_scroll addSubview:_MontherDoTableview];
    self.MontherDoTableview.delegate = self;
    self.MontherDoTableview.dataSource = self;
    self.MontherDoTableview.rowHeight = 300;
    
    
    //轮播图
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH,HEIGHT / 4)];
    self.scrollView.backgroundColor = [UIColor yellowColor];
    [_MontherLookHeaderview addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(9 * WIDTH, 0);
    self.scrollView.pagingEnabled = YES;
    
    UIImageView *lastImageView = [[UIImageView alloc]initWithFrame:CGRectMake(8 * WIDTH, 0,WIDTH, HEIGHT / 4)];
    lastImageView.image = [UIImage imageNamed:@"h1.jpeg"];
    [self.scrollView addSubview:lastImageView];
    lastImageView.userInteractionEnabled = YES;
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    
    UIImageView *firstimage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"h7.jpeg"]];
    firstimage.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 4);
    [self.scrollView addSubview:firstimage];
    firstimage.userInteractionEnabled = YES;
    
    for (NSInteger i = 1; i < 8; i ++) {
        NSString *imageName = [NSString stringWithFormat:@"h%ld.jpeg", i];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT / 4);
        [self.scrollView addSubview:imageView];
        imageView.userInteractionEnabled = YES;
    }
    self.scrollView.delegate = self;

    for (int i = 1; i < 7; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(2 * WIDTH + i * 50, 20, 50, 50);
        [_scroll addSubview:button];
        [button setTitle:@"一" forState:UIControlStateNormal];
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 25;
        button.layer.masksToBounds = YES;
        [button addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1050 + i;
    }
    
}

//Monthertableview协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 2) {
//        int x = arc4random()%self.picArr.count + 1;
//        if (x == 1) {
//            return 200;
//        }else {
//           return 200 + x / 2 *100;
//        }
//    }
//    else{
//        return 450;
//    }
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _MontherLookTableview) {
        static NSString *MontherLooks = @"tableView";
        MontherLookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MontherLooks];
        if (!cell) {
            cell = [[MontherLookTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MontherLooks];
            }
        cell.LabelLeftimage.image = [UIImage imageNamed:@"lableLeftImage"];
        [cell.rightButton setTitle:@"大牛妈说 >" forState:UIControlStateNormal];
        cell.topImage.image = [UIImage imageNamed:@"topImage"];
        cell.leftImage.image = [UIImage imageNamed:@"bottomImage"];
        cell.rightImage.image = [UIImage imageNamed:@"bottomImage"];
        cell.bottomLeftImage.image = [UIImage imageNamed:@"Middle"];
        cell.bottomRightImage.image = [UIImage imageNamed:@"Middle"];
        return  cell;
        }
    if (tableView == _MontherDoTableview) {
        static NSString *MontherLooks = @"MontherDotableView";
        MontherDoTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:MontherLooks];
        if (!cells) {
            cells = [[MontherDoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MontherLooks];
        }
        cells.backImages.image = [UIImage imageNamed:@"topImage"];
        return cells;
    }
    
    
    return nil;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击");
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//点击后不显示效果
}

//滑动结束后触动的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView) {
        if (scrollView.contentOffset.x == 8 * WIDTH) {
            self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
        }
        if (scrollView.contentOffset.x == 0) {
            self.scrollView.contentOffset = CGPointMake(7 * WIDTH, 0);
        }
        
    }
    if (scrollView == _scroll) {
        if (scrollView.contentOffset.x == 0) {
            [_MontherReadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_MontherDoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_MontherLookButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        }
        if (scrollView.contentOffset.x == WIDTH) {
            [_MontherDoTableview reloadData];
            [_MontherLookButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_MontherReadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_MontherDoButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        }
        if (scrollView.contentOffset.x == 2 * WIDTH) {
            [_MontherLookButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_MontherDoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_MontherReadButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        }
    }
}

- (void)buttonclick:(UIButton *)click {

    UIView *views = [[UIView alloc]initWithFrame:CGRectMake(2 * WIDTH, 100, WIDTH, 500)];
    [_scroll addSubview:views];
    if (click.selected == NO && click.tag == 1051) {
        
        views.backgroundColor = [UIColor blueColor];
    }
    if (click.selected == NO && click.tag == 1052) {
        views.backgroundColor = [UIColor redColor];
    }
    if (click.selected == NO && click.tag == 1053) {
        views.backgroundColor = [UIColor grayColor];
    }
    if (click.selected == NO && click.tag == 1054) {
        views.backgroundColor = [UIColor greenColor];
    }
    if (click.selected == NO && click.tag == 1055) {
        views.backgroundColor = [UIColor orangeColor];
    }
    if (click.selected == NO && click.tag == 1056) {
        views.backgroundColor = [UIColor whiteColor];
    }
    NSLog(@"%ld", click.tag);

}
- (void)LookClick:(UIButton *)look {
    self.scroll.contentOffset = CGPointMake(0, 0);
    [_MontherReadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_MontherDoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_MontherLookButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
- (void)DoClick:(UIButton *)doClick {
    self.scroll.contentOffset = CGPointMake(WIDTH, 0);
    [_MontherLookButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_MontherReadButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_MontherDoButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
- (void)readClick:(UIButton *)read {
    self.scroll.contentOffset = CGPointMake(2 * WIDTH, 0);
    [_MontherLookButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_MontherDoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_MontherReadButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
